package com.hzcx.app.simplechat.ui.group;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Switch;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.chat.ChatGroupSettingActivity;
import com.hzcx.app.simplechat.ui.friend.FriendListByLableActivity;
import com.hzcx.app.simplechat.ui.friend.bean.FriendBean;
import com.hzcx.app.simplechat.ui.friend.bean.FriendCityBean;
import com.hzcx.app.simplechat.ui.group.LiveData.GroupInfoBeanLiveData;
import com.hzcx.app.simplechat.ui.group.adapter.GroupMembersSort;
import com.hzcx.app.simplechat.ui.group.bean.GroupInfoBean;
import com.hzcx.app.simplechat.ui.group.bean.ManagerMembersBean;
import com.hzcx.app.simplechat.ui.group.bean.MembersBean;
import com.hzcx.app.simplechat.ui.home.AddFriendActivity;
import com.hzcx.app.simplechat.ui.mine.MineUserInfoActivity;
import com.hzcx.app.simplechat.ui.publicui.ExpandGridView;
import com.hzcx.app.simplechat.ui.publicui.adapter.GroupSettingGridAdapter;
import com.hzcx.app.simplechat.ui.user.UserInfoActivity;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.toast.ToastUtils;

import org.litepal.LitePal;
import org.litepal.crud.callback.FindMultiCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class GroupMangersActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.setting_group_manager_list)
    ExpandGridView mAvatarsEgv;

//    public static String GroupInfoBeanKey = "GroupMangersActivityGroupInfoBean";
//    GroupInfoBean groupInfoBean;
    public static String GroupInfoBeanKeyGroupID = "QrCodeActivity_GroupInfoBeanKeyGroupID";
    GroupInfoBean groupInfoBean;
    private String groupID;

    private GroupSettingGridAdapter mGroupSettingGridAdapter;
    private List<MembersBean> adminMembersBeans = new ArrayList<>();              // 管理员列表
    private List<MembersBean> ordinaryMembersBeans = new ArrayList<>();           // 普通用户列表




    @Override
    public int getLayout() {
        return R.layout.activity_group_mangers;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("群管理员");
    }

    @Override
    protected void onResume() {
        super.onResume();

        //        groupInfoBean = (GroupInfoBean)getIntent().getSerializableExtra(GroupInfoBeanKey);
        groupID = getIntent().getStringExtra(GroupInfoBeanKeyGroupID);
        groupInfoBean = GroupInfoBeanLiveData.getGroupInfoBean(groupID);
        getGroupManagers();

        if (groupInfoBean == null) { return; }
        adminMembersBeans.clear();
        ordinaryMembersBeans.clear();
        for (MembersBean bean : groupInfoBean.getMembers()) {
            if (bean.getIs_admin().equals("Y")) {
                adminMembersBeans.add(bean);
            } else {
                ordinaryMembersBeans.add(bean);
            }
        }

        mGroupSettingGridAdapter = new GroupSettingGridAdapter(this, adminMembersBeans);
        if (groupInfoBean.getMy().getIs_owner().equals("Y")) {
            mGroupSettingGridAdapter.showAdd = true;
            if (adminMembersBeans.size() == 0) {
                mGroupSettingGridAdapter.showDele = false;
            } else {
                mGroupSettingGridAdapter.showDele = true;
            }
        } else {
            mGroupSettingGridAdapter.showAdd = false;
            mGroupSettingGridAdapter.showDele = false;
        }

        mAvatarsEgv.setAdapter(mGroupSettingGridAdapter);
        mAvatarsEgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i == adminMembersBeans.size()) { // 邀请管理员
                    List<FriendBean> beans = GroupMembersSort.sortFriend(ordinaryMembersBeans);
                    Intent intent = new Intent(GroupMangersActivity.this, GroupManagerAddActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(FriendListByLableActivity.INTENT_LIST_BUNDLE, beans.toArray());
                    intent.putExtras(bundle);
                    intent.putExtra(GroupCreateActivity.GroupCreateActivityGroupID, groupInfoBean.getGroup_id());
                    startActivity(intent);
                } else if (i == adminMembersBeans.size() + 1) {  // 删除群管理员
                    List<FriendBean> beans = GroupMembersSort.sortFriend(adminMembersBeans);
                    Intent intent = new Intent(GroupMangersActivity.this, GroupManagerDeleteActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(FriendListByLableActivity.INTENT_LIST_BUNDLE, beans.toArray());
                    intent.putExtras(bundle);
                    intent.putExtra(GroupCreateActivity.GroupCreateActivityGroupID, groupInfoBean.getGroup_id());
                    startActivity(intent);
                } else {    // 查看好友详情
                    MembersBean membersBean = adminMembersBeans.get(i);
                    if (membersBean.getMember_id() == UserInfoUtil.getUserId()) {   // 点击的群成员是自己
                        startActivity(new Intent(GroupMangersActivity.this, MineUserInfoActivity.class));
                        return;
                    }

                    LitePal.findAllAsync(FriendBean.class, true).listen(new FindMultiCallback<FriendBean>() {
                        @RequiresApi(api = Build.VERSION_CODES.N)
                        @Override
                        public void onFinish(List<FriendBean> list) {
                            for (FriendBean friend: list) {
                                for (FriendCityBean friendCityBean: friend.getCity()) {
                                    if (friendCityBean.getMember_id() == membersBean.getMember_id()) {  // 好友
                                        startActivity(new Intent(GroupMangersActivity.this, UserInfoActivity.class)
                                                .putExtra(UserInfoActivity.INTENT_USER_ID, membersBean.getMember_id()));
                                        return;
                                    }
                                }
                            }
                            //  陌生人
                            Intent intent = new Intent(GroupMangersActivity.this, AddFriendActivity.class);
                            intent.putExtra(AddFriendActivity.INTENT_USER_ID, membersBean.getMember_id());
                            intent.putExtra(AddFriendActivity.INTENT_TYPE, 2);
                            if (groupInfoBean.getProtectedswitch() == 1) {
                                intent.putExtra(AddFriendActivity.PROTECT_GROUP_MEMBER, true);
                            }
                            startActivity(intent);
                        }
                    });
                }
            }
        });
    }

    private void getGroupManagers() {
//        GroupModel.getGroupManagers(this, groupID, "1", "100", new BaseObserver<List<ManagerMembersBean>>() {
//            @Override
//            protected void onSuccess(List<ManagerMembersBean> data) {
////                ToastUtils.show(data.toString());
//            }
//        });

    }

    @Override
    public void initData() {

    }
}